package com.example.HQCource;






import org.junit.Test;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Random;

public class addsql {
    @Test
   public void Demo(){
        System.out.println(new Random().nextInt(6));
    }

    private String Driver = "com.mysql.cj.jdbc.Driver";
    private String url ="jdbc:mysql://localhost:3306/glkt_vod?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
    private String user = "root";
    private String password = "123456";
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    public void coon() throws Exception{
        Class.forName(Driver);
        connection = (Connection) DriverManager.getConnection(url,user,password);
    }
    public void erro(){
        try {
            if (rs!=null){
                rs.close();
            }
            if (ps!=null){
                ps.close();
            }
            if (connection!=null){
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void ccc(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        int ao=0;
        String ls="";
        String ls1="";
        String[] str={"李","刘","孙","马","子","赵","王","朱","罗","钱","蔡","佳","文文","周","诸葛","曹"};
        String[] str2={"高级讲师","中级讲师",str[ao]+"老师",};
        String[] str1={"a","b","c","d","f","q","l","z","w","p","o","u","y","t","m","n","b","v","c"};
        String[] str3={"a","b","c","d","f","q","l","z","w","p","o","u","y","t","m","n","b","v","c"
        ,"A","V","C","D","W","Q","T","T","W","T","UI","V","B",
        "1","2","3","5","4","6","7","8","9"};
        String[] data={"2021","2010","2023","2022","2009"};
        String[] yue={"1","2","3","4","5","6","7","8","9","10","11","12"};
        long start = System.currentTimeMillis();
        String sql = "insert into teacher(id,name,intro,career,level,avatar,sort,join_date,create_time,update_time) VALUES (?,?,?,?,?,?,?,?,?,?)";
        try {
            coon();
            ps = connection.prepareStatement(sql);
            for (int i = 10; i <= 10000; i++) {
                int uo = new Random().nextInt(str.length);
                ao=uo;
                int i1 = new Random().nextInt(str2.length);
                while (ls.length()<=6) {
                      ls+= str1[new Random().nextInt(str2.length)];
                }
                while (ls1.length()<=20) {
                   ls1+=str3[new Random().nextInt(str3.length)];
                }
                ps.setObject(1, i);//填充sql语句种得占位符
                ps.setObject(2,str[uo]+"老师");
                ps.setObject(3,str2[i1]);
                ps.setObject(4,str2[i1]);
                ps.setObject(5,new Random().nextInt(2));
                ps.setObject(6,"https://"+ls+"/"+ls1);
                ps.setObject(7,new Random().nextInt(6));
                ps.setObject(8,simpleDateFormat.parse(data[new Random().nextInt(data.length)]+"-"+yue[new Random().nextInt(yue.length)]+"-"
                                                             +yue[new Random().nextInt(yue.length)]));
                ps.setObject(9,null);
                ps.setObject(10,null);
                ps.execute();//执行sql语句
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            erro();
        }
        System.out.println("百万条数据插入用时：" + (System.currentTimeMillis() - start)+"【单位：毫秒】");

    }


}


